//
//  TJTransferRecordListController.m
//  Teleport
//
//  Created by xtjun on 2020/6/13.
//  Copyright © 2020 xtjun. All rights reserved.
//

#import "TJTransferRecordListController.h"
#import "TJTransferRecordCell.h"

@interface TJTransferRecordListController ()


@property (strong, nonatomic) NSMutableArray *datas;


@property (assign, nonatomic) int pageIndex;


@end

static NSString * const transferRecordCell = @"transferRecordCell";

@implementation TJTransferRecordListController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBar.title = @"资产记录".localizedString;
    
    _datas = @[].mutableCopy;
    self.view.backgroundColor = UIColor.whiteColor;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.rowHeight = 74;
    self.tableView.backgroundColor = UIColor_Hex(0xf2f2f2);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsHorizontalScrollIndicator = NO;
     self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView registerClass:TJTransferRecordCell.self forCellReuseIdentifier:transferRecordCell];
     [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
          make.top.offset(KNavBarHight);
          make.left.right.bottom.offset(0);
      }];
    
        [self setUpData];
}
- (void)setUpData{
    [self loadNewData];

    self.tableView.mj_header = self.refreshHeader;
    self.tableView.mj_footer = self.refreshFooter;
 
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TJTransferRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:transferRecordCell forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.model = self.datas[indexPath.row];
    return cell;
}

- (void)loadNewData{
    _pageIndex = 1;
 
    NSDictionary *parameters = @{

        @"size": @(10),
        @"userId": TJUserInfo.readInfo.userId,
        @"page": @(_pageIndex)
     
    };
    
   
    
    weakify(self);
    [TJNetworking.shareNetworking GET:@"member/transfer/getRecord" parameters:parameters success:^(NSDictionary * _Nonnull responseObject, NSDictionary *data) {
        
        weakself.displayEmpty = YES;
        
        [weakself.datas removeAllObjects];
        weakself.datas = [TJTransferRecordModel mj_objectArrayWithKeyValuesArray:data[@"record"]];
    
        [weakself.tableView reloadData];
        [weakself footerViewIsHidden:weakself.datas];
        [weakself.tableView.mj_header endRefreshing];
    } failure:^(NSError * _Nonnull error) {
         weakself.displayEmpty = YES;
        [weakself.tableView reloadData];
        [weakself.tableView.mj_header endRefreshing];
    }];
    
}

- (void)loadMoreData {
    _pageIndex++;
    
   
    NSDictionary *parameters = @{
        @"size": @(10),
        @"userId": TJUserInfo.readInfo.userId,
        @"page": @(_pageIndex)
     
    };
    

    weakify(self);
    [TJNetworking.shareNetworking GET:@"member/transfer/getRecord" parameters:parameters success:^(NSDictionary * _Nonnull responseObject, NSDictionary *data) {
        
        NSArray *arr = [TJTransferRecordModel mj_objectArrayWithKeyValuesArray:data[@"record"]];
        
        [weakself.datas addObjectsFromArray:arr];
         [weakself footerViewIsHidden:arr];
        [weakself.tableView reloadData];
        [weakself.tableView.mj_footer endRefreshing];
        
        
    } failure:^(NSError * _Nonnull error) {
        [weakself.tableView.mj_footer endRefreshing];
    }];
}


- (void)footerViewIsHidden:(NSArray *)arr {
    if (arr.count < 10) {
        self.tableView.mj_footer.hidden = YES;
    }else{
        self.tableView.mj_footer.hidden = NO;
    }
}


@end
